<?php

namespace App\Repositories;

use App\Lib\ApiResponse;
use App\Models\Report;
use App\Models\Setting;
use App\Models\Staff;
use App\Models\Wxusers;

class ManagerRepository
{
    public function getReportList($mall_id)
    {
        $data = request()->all();


        $main_query = Report::query()->leftJoin('wxusers','wxusers.id','=','reports.wxuser_id')
            ->select([
                'reports.id','reports.foot_rid','reports.foot_puid','reports.wx_openid','reports.created_at','reports.sync_status',
                'wxusers.wx_nickname','wxusers.wx_headimgurl'
            ])
            ->where('reports.mall_id',$mall_id)
            ->where('reports.wxuser_id','>',0);

        if(!empty($data['nick_name'])){
            $main_query->where('wxusers.wx_nickname', $data['nick_name']);
        }
        if(!empty($data['rid'])){
            $main_query->where('reports.foot_rid', $data['rid']);
        }
        if(!empty($data['start_created'])){
            $main_query->where('reports.created_at', '>=', $data['start_created']);
        }
        if(!empty($data['end_created'])){
            $main_query->where('reports.created_at', '>=', $data['end_created']);
        }

        return $main_query->orderBy('id','desc')->paginate()->toArray();
    }

    public function getStaffList()
    {
        $data = request()->all();
        $main_query = Staff::query();

        if(!empty($data['nick_name'])){
            $main_query->where('wx_nickname', $data['nick_name']);
        }
        if(!empty($data['openid'])){
            $main_query->where('wx_openid', $data['openid']);
        }
        if(!empty($data['start_created'])){
            $main_query->where('created_at', '>=', $data['start_created']);
        }
        if(!empty($data['end_created'])){
            $main_query->where('created_at', '>=', $data['end_created']);
        }

        return $main_query->orderBy('id','desc')->paginate()->toArray();
    }

    public function getWxUserList($mall_id)
    {
        $data = request()->all();
        $main_query = Wxusers::query()->where('mall_id', $mall_id);

        if(!empty($data['nick_name'])){
            $main_query->where('wx_nickname', $data['nick_name']);
        }
        if(!empty($data['openid'])){
            $main_query->where('wx_openid', $data['openid']);
        }
        if(!empty($data['start_created'])){
            $main_query->where('created_at', '>=', $data['start_created']);
        }
        if(!empty($data['end_created'])){
            $main_query->where('created_at', '>=', $data['end_created']);
        }

        return $main_query->orderBy('id','desc')->paginate()->toArray();
    }

    public function getSetting($mall_id)
    {
        return Setting::query()->where('mall_id', $mall_id)->first();
    }

    public function saveReportSetting($mall_id)
    {
        $data = \request()->post();
        if(empty($data['wx_appid']) || empty($data['wx_appsecret']) || empty($data['foot_puid'])){
            return ApiResponse::failJson('参数错误');
        }

        Setting::updateOrCreate(['mall_id' => $mall_id],[
            'wx_appid' => $data['wx_appid'],
            'foot_puid' => $data['foot_puid'],
            'wx_appsecret' => $data['wx_appsecret'],
            'wx_token' => $data['wx_token'],
            'wx_sea_key' => $data['wx_sea_key'],
        ]);

        return ApiResponse::successJson($data);
    }
}
